package com.gzl.autotest.utils;

import com.gzl.autotest.entity.CaseData;
import com.gzl.autotest.entity.Variable;
import com.gzl.autotest.testcase.BaseCase;
import org.apache.log4j.Logger;
import org.testng.Assert;

import java.util.List;
import java.util.regex.Matcher;
public class GlobalVariableUtil {
    public static Logger logger = Logger.getLogger(GlobalVariableUtil.class);




    /**
     * 从全局map中获取全局变量并替换
     * @param param
     * @return
     */
    public static String substitutionGlobalVariable(String param){
        if (!StringUtil.isNotNullAndEmpty(param)){
            return "";
        }
        Matcher matcher = BaseCase.replaceParamPattern.matcher(param);
        while (matcher.find()) {
            String replaceKey = matcher.group(1).trim();  // 得到第一个匹配内容
            String value;
            // 从全局变量map中获取值
            if ("".equals(replaceKey) || !BaseCase.globalVariableMap.containsKey(replaceKey)){
                value = null;
                Assert.fail("替换失败");  // 如果未能找到对应的值，该用例失败
            } else {
                value = BaseCase.globalVariableMap.get(replaceKey);
                // logger.info("value: " + value);
            }
            param = param.replace(matcher.group(), value);
        }
        return param;
    }
}